#include <iostream>
#include <string>
#include "../include/thread_pool.h"

void print_thread_id() {
    static std::mutex io_mutex;
    std::lock_guard<std::mutex> lk(io_mutex);
    std::cout << std::this_thread::get_id() << std::endl;
}

std::string work1(int a, std::string b) {
    print_thread_id();
    return std::to_string(a) + b;
}

void work2() {
    print_thread_id();
}

int work3(int& a, int& b) {
    int c = a + b;
    a = b = 0;
    return c;
}

int main(int argc, char* argv[]) {
    auto res1 = thread_pool::globalInstance()->submit(work1, 1, "2");
    auto res2 = thread_pool::globalInstance()->submit(work2);
    std::cout << res1.get() << std::endl;
    //res2.get();   // 如果不 get 的话, 就不知道任务是否在程序结束前完成
    int a = 2, b = 3;
    auto res3 = thread_pool::globalInstance()->submit(work3, std::ref(a), std::ref(b));
    std::cout << res3.get() << ' ' << a << ' ' << b << std::endl; // 5 0 0

    std::vector<std::future<void>> futures(10);
    for (int i = 0; i < 10; i++) {
        futures[i] = thread_pool::globalInstance()->submit(work2);
    }

    std::for_each(futures.begin(), futures.end(), [](std::future<void>& f){
        f.get();
    });

    return 0;
}